﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace Puman
{
    public class VCode
    {
        public static string GenerateCheckCode(int minLen, int maxLen, out MemoryStream ms)
        {
            string checkCode = String.Empty;
            System.Random random = new Random();
            int n = random.Next(minLen, maxLen);

            string so = "1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z";//,a,b,c,d,e,f,g,h,i,j,k,m,c,p,q,r,s,u,v,w,x,y,z";
            string[] strArr = so.Split(',');
            for (int i = 0; i < n; i++)
            {
                checkCode += strArr[random.Next(0, strArr.Length)];
            }

            ms = CreateCheckCodeImage(checkCode);
            return checkCode;
        }

        private static MemoryStream CreateCheckCodeImage(string checkCode)
        {
            if (checkCode == null || checkCode.Trim() == String.Empty)
                return null;

            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 22.5)), 30);
            Graphics g = Graphics.FromImage(image);

            try
            {
                //生成随机生成器
                Random random = new Random();

                //清空图片背景色
                g.Clear(Color.White);

                //定义颜色   
                Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
                //定义字体   
                // string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial" };
                string[] font = { "Arial" };

                //画图片的背景噪音线
                for (int i = 0; i < 5; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);
                    int cindex = random.Next(7);
                    g.DrawLine(new Pen(c[cindex], 1), x1, y1, x2, y2);

                    if (i == 2)
                    {
                        g.DrawLine(new Pen(c[cindex], 2), x1, y1, x2, y2);
                    }
                }

                char[] chars = checkCode.ToCharArray();//拆散字符串成单字符数组   
                //文字距中   
                StringFormat format = new StringFormat(StringFormatFlags.NoClip);
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;


                int randAngle = 15; //随机转动角度   
                //偏移文字位置。不同的字体。倾斜文字
                for (int i = 0; i < chars.Length; i++)
                {
                    int cindex = random.Next(7);
                    int findex = random.Next(font.Length);
                    int fsize = random.Next(16, 18);

                    Font f = new System.Drawing.Font(font[findex], fsize, System.Drawing.FontStyle.Regular);//字体样式(参数2为字体大小)   
                    Brush b = new System.Drawing.SolidBrush(c[cindex]);
                    Point dot = new Point(random.Next(12, 16), random.Next(10, 20));

                    float angle = random.Next(-randAngle, randAngle);//转动的度数   
                    g.TranslateTransform(dot.X, dot.Y);//移动光标到指定位置   
                    g.RotateTransform(angle);
                    g.DrawString(chars[i].ToString(), f, b, 1, 1, format);
                    //g.DrawString(chars[i].ToString(),fontstyle,new SolidBrush(Color.Blue),1,1,format);   
                    g.RotateTransform(-angle);//转回去   
                    g.TranslateTransform(2, -dot.Y);//移动光标到指定位置   


                    //画图片的前景噪音线
                    //int x1 = random.Next(image.Width);
                    //int x2 = random.Next(image.Width);
                    //int y1 = random.Next(image.Height);
                    //int y2 = random.Next(image.Height);

                    //g.DrawLine(new Pen(c[cindex]), x1, y1, x2, y2);

                }

                //画图片的前景噪音点
                for (int i = 0; i < 150; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }


                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                return ms;
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }

        }
    }
}
